package vn.gq.tym.utils;

import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;


public class ClientIO {
	
	public abstract class ClientProgressListener {
		public abstract void onPostProgress(int value);
	}
	
	public static String convertStreamToString(InputStream is, ClientProgressListener listener) {
		StringBuilder out = new StringBuilder();
		int byteRead = 0;
		byte[] buffer = new byte[8096];
		int totalByteRead = 0;
		
		try {
			while ((byteRead = is.read(buffer)) != -1) {
				out.append(new String(buffer, 0, byteRead));
				totalByteRead += byteRead;
				if (listener != null) listener.onPostProgress(totalByteRead);
			}
			is.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return out.toString();
	}
	
	public static void copy(InputStream is, OutputStream os, ClientProgressListener listener) {
		int byteRead = 0;
		byte[] buffer = new byte[8096];
		int totalByteRead = 0;
		
		try {
			while ((byteRead = is.read(buffer)) != -1) {
				os.write(buffer, 0, byteRead);
				totalByteRead += byteRead;
				if (listener != null) listener.onPostProgress(totalByteRead);
			}
			is.close();
			os.flush();
			os.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void copy(InputStream is, String path, ClientProgressListener listener) {
		try {
			BufferedOutputStream os = new BufferedOutputStream(new FileOutputStream(path));
			copy(is, os, listener);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static InputStream getCache(File fileCacheDir, String key, boolean clear) {
		String cacheFilePath = fileCacheDir.getAbsolutePath() + File.separator + key;
		File f = new File(cacheFilePath);
		if (!f.exists()) return null;
		try {
			if (!clear && f.lastModified()<System.currentTimeMillis()) {
				return null;
			}
			return new FileInputStream(f);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static InputStream cacheToSdcard(File fileCacheDir, String key, InputStream is, final long expire) {
		if (is == null || ("".equals(key))) return null;
		if (!fileCacheDir.exists()) fileCacheDir.mkdirs();
		try {
			final ByteArrayOutputStream os = new ByteArrayOutputStream();
			int byteRead = 0;
			byte[] buffer = new byte[4096];
			while(0<(byteRead = is.read(buffer))) {
				os.write(buffer, 0, byteRead);
			}
			os.close();
			final String cacheFilePath = fileCacheDir.getAbsolutePath() + File.separator + key;
			Thread thread = new Thread(new Runnable() {
				
				@Override
				public void run() {
					File f = new File(cacheFilePath);
					FileOutputStream fos;
					try {
						fos = new FileOutputStream(f);
						fos.write(os.toByteArray());
						fos.flush();
						fos.close();
						f.setLastModified(expire);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					} catch (IOException e) {
						e.printStackTrace();
					}
					
				}
			});
			thread.setPriority(Thread.MIN_PRIORITY);
			thread.start();
			return new ByteArrayInputStream(os.toByteArray());
		} catch (IOException e) {
			e.printStackTrace();
			return is;
		}
	}
	
}
